#include "debugwebengine.h"

debugwebengine::debugwebengine(QObject* parent): QWebEnginePage(parent)
{
    //qDebug() << "debugwebengine initialized.";
     connect(this, &QWebEnginePage::featurePermissionRequested, this, &debugwebengine::onFeaturePermissionRequested);

}
//重写这个函数以保证它跳过ssl证书验证
bool debugwebengine::certificateError(const QWebEngineCertificateError &certificateError)
{
    return true;
}
void debugwebengine::onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature) {
    //qDebug() << "Granted permission for feature:" ;
        // 检查请求的功能权限
        if (feature == QWebEnginePage::MediaAudioCapture || feature == QWebEnginePage::MediaVideoCapture) {
            // 批准摄像头和麦克风的访问权限
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
            qDebug() << "Granted permission for feature:" << feature << "from origin:" << securityOrigin.toString();
        } else {
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);//所有权限都给
            qDebug() << "Denied permission for feature:" << feature << "from origin:" << securityOrigin.toString();
        }
    }

void debugwebengine::javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) {
    // 将 JavaScript 的 console 输出转发到 Qt 的日志系统
    QString logMessage = QString("JS Console: %1 (Line: %2, Source: %3)").arg(message).arg(lineNumber).arg(sourceID);
    switch (level) {
        case InfoMessageLevel:
            qInfo() << logMessage;
            break;
        case WarningMessageLevel:
            qWarning() << logMessage;
            break;
        case ErrorMessageLevel:
            qCritical() << logMessage;
            break;
        default:
            qDebug() << logMessage;
            break;
    }
}
